package sort;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

/**
 *
 * @author BDSHADOW
 */
public class BigReadWrite {
    static long size = 0;
    public int[] readArray(String from) {
         MappedByteBuffer reader = null;
         FileChannel channel = null;
         int numbers[] = null;
         
         try {
         channel = new RandomAccessFile(from, "r").getChannel();
         long filesize = channel.size(); 
         size = filesize;
         reader = channel.map(FileChannel.MapMode.READ_ONLY, 0, filesize);
         channel.close();
         
         reader.position(0);
         StringBuffer string = new StringBuffer();
         while (reader.hasRemaining()) {
             string.append((char)reader.get());
         }
         String[] strNumbers = string.toString().split(" ");
         numbers = new int[strNumbers.length];
         for (int i = 0; i < strNumbers.length; i++) {
             numbers[i] = Integer.valueOf(strNumbers[i]);
         }
         return numbers;
         } catch (FileNotFoundException e) {
             System.out.println("File wasn't found");
         } catch (IOException e) {
             System.out.println("File can't be read");
         }
         return numbers;
     }
     
     public void writeArray(int[] arr, String src) {
         MappedByteBuffer writer = null;
         FileChannel channel = null;
         
         try {
         channel = new RandomAccessFile(src, "rw").getChannel();
         writer = channel.map(FileChannel.MapMode.READ_WRITE, 0, size + 1);
         channel.close();
         
         writer.position(0);
         for (int i = 0; i < arr.length; i++) {
             ByteBuffer num = ByteBuffer.wrap(String.valueOf(arr[i] + " ").getBytes());
             writer.put(num);
         }/*
         for (int i = 0; i < arr.length; i++) {
             writer.putInt(arr[i]);
         }
         writer.force();
         
         writer.position(0);
         for (int i = 0; i < arr.length; i++) {
             writer.getInt();
             System.out.println(arr[i]);
         }*/
         
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
}
